<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
  </head>
  <body>
    bind的作用和异同点
  </body>
  <script>
    var foo = {
      value: 1
    };
    function bar(name) {
      console.log(this.value);
      console.log(name)
    }
    // 返回了一个函数
    // var bindFoo = bar.bind2(foo, 'yyyyy');
    // bindFoo(); // 1


    Function.prototype.bind2 = function (context) {

        if (typeof this !== "function") {
          throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
        }

        var self = this;
        var args = Array.prototype.slice.call(arguments, 1);
        var fNOP = function () {};

        var fbound = function () {
            self.apply(this instanceof self ? this : context, args.concat(Array.prototype.slice.call(arguments)));
        }

        fNOP.prototype = this.prototype;
        fbound.prototype = new fNOP();

        return fbound;

    }

    console.log((!(~+[]) + {})[--[~+""][+[]] * [~+[]] + ~~!+[]] + ({} + [])[[~!+[]] * ~+[]])

  </script>
</html>
